/*Replication file for Cook et al. "The short-run impacts of reducing water collection 
times...", Economic Development and Cultural Change.  

July 2023


This file replicates tables and figures reporting time use
and affect changes for water collectors. It uses four datasets:

1.Household_DeID: household level variables
2. ESM_DeID: survey records generated through ESM exercise
3. Schoolkids_DeID: survey records on daily activities of school-aged children
4. SchoolAttendance_DeId: school attendance records

Created and run in Stata 15.1

Needs commands "leebounds" for Lee Bounds and "wolf2" for Romano-Wolf

!!!Tables and figures are saved into subfolders "textables" and "texfigures", which
MUST be created under the working directory for this code to run!!!.*/


/*Set working directory here*/

cd "C:\...."    

clear all




/*Household and water-collector level balance tables*/
use Household_DeID, clear

/*Table 1 : Main water collector Balance table*/
balancetable treat female age read_diff illit educ ///
	 ownfarm work_base privbus workdays dailywages ///
	 timert timewait volume ///
	using textables/mwc_balance.tex, ///
	ctitles("Control" "Treatment" "Difference") ///
	format(%5.2f) replace varlabels nonumbers 

	
/*Table 2:  Household Balance tables*/
balancetable treat incomemid hhwages2weeks w_index dirtfloor ///
	buildings land electricity ///
	storagetotal treatwater vendliters othermobile  ///
	using textables/hh_balance.tex, ///
	ctitles("Control" "Treatment" "Difference") ///
	format(%7.2g) replace varlabels wrap



use "ESM_DeID", clear 

rename treat Treat



		
**************************************************
********  Include "over-submitters"? *************
	

/*FLAG captures records that are suspicious for "over-submitting"*/
tab FLAG
*generate variables for main or extended dataset
gen sample0=0
	replace sample0=1 if FLAG==0 /*unflagged data*/
gen sample1=1  /*all data*/
	
	tab FLAG sample0 /*unflagged data, n=14,223*/
	tab FLAG sample1 /*all data, n=16,885*/

	/*Lengthmins drop - uncomment for sensitivity analysis
	drop if lengthmins>15 */
	
	
	
/*Set flag on or off here. 
		0 = exclude the over-submitters, main analysis
		1 = include the over-submitters, online appendix */	
	global i 0
	display  $i
	
**************************************************
**************************************************


	/*how many households */
		preserve
		 collapse (mean) start Treat sample$i, by(phoneid)
		 *tab phoneid  
		 tab Treat
		restore



/* Figure 2. ESM submissions - overall pattern 

/*submissions by day*/
hist startday if sample$i==1, freq title(Panel A: Submissions by date) ///
	 ytitle("Number of submissions") ///
	 bin(43) scheme(s1mono)  ///
	 ylab(0(200)600) ///
	 name(A, replace) 
	 gr export "texfigures/ESMhist_$i.emf", as(emf) replace
	/*43 days in period aug 27-oct8, including sundays off*/
	/*note Figure 3 is also based on this histogram, with study dates manually 
	added as graphics*/
	 
	 
/*average submissions per day*/
twoway (line ESMcountALL_$i startday if sample$i==1, sort nodraw ///
	scheme(s1mono) ///
	ytitle("Submissions" "per day") ///
	name(B,replace) title("Panel B: Avg submissions per day") )

		
/*time of day*/
label var endhr "Hour (0=midnight)"
hist endhr if sample$i==1, discrete percent nodraw ///
	ytitle("Percent of" "total responses") ///
	xlabel(0(4)24) ///
	scheme(s1mono) ///
	title(Panel C: Time of day completed) name(C, replace)
	
/*submissions by day of week*/
graph bar if sample$i==1, over(endday_of_week) ///
	title("Panel D: Submissions by day of week") ///
	ytitle("Percent of" "total responses") ///
	scheme(s1mono) ///
	nodraw name(D, replace)	
	
/*survey length*/
hist lengthmins if sample$i==1 & lengthmins<=15, title(Panel E: Time to complete survey) ///
	ytitle("Percent of" "total responses") ///
	scheme(s1mono) ///
	 percent nodraw name(E, replace)
	 

	 sum lengthmins if sample$i==1, det
	 sum lengthmins if lengthmins>15 & sample$i==1
	 
gr combine A B C D E, scheme(s1color) cols(2) iscale(0.6)
		gr export "texfigures/ESMdiagnostics_$i.pdf", replace
		gr export "texfigures/ESMdiagnostics_$i.emf", as(emf) replace
*/
		
		
/*Startday - above there were a few instances where the start of the survey was in an 
implausible year (database error), and set to blank. But below we use startday 
to set post*trt, so we need to fill something in for these. Assume startday same 
as endday.  */
codebook startday  /*55 of 16,885*/
replace startday = endday if startday==.
drop if startday==. /*one additional blank record here*/

			
/*day of week control*/
gen dow=dow(startday)
gen week=week(startday)

 /*"Post" and "Post*treat" variables for diff-in-diff*/
	
	/*Excludes two weeks of partial vending, sets to missing for control & trt*/
	gen POSTITT = startday>=td(26sept2016)  
			replace POSTITT=. if startday>=td(12sept2016) & startday<=td(26sept2016)  
			label var POSTITT "Post"
	gen POSTITT_TRT = Treat*POSTITT
			label var POSTITT_TRT "Post*Treat"

		
/*Merge in household-level data*/
merge m:1 phoneid using "Household_DeID"
*rename morn morn_base

		
/*time of day controls*/
for any morn aft even: gen X=0
replace morn=1 if endhr<=12
replace aft=1 if endhr>12 & endhr<=17
replace even=1 if endhr>17
label var morn "Morning (5am-noon)"
label var aft "Afternoon (noon-5pm)"
label var even "Evening (after 5pm)"
		
		
/***********Water-sharing************/	

/*do trt households who reported sharing (mid or end) still collect water?*/
gen trtshare=0
replace trtshare=1 if vendgroupsharetreat_mid==1| vendgroupsharetreat_end==1

	preserve
		keep if sample$i==1 & Treat==1
		collapse (mean) trtshare, by(phoneid)
		tab trtshare  
	restore



gen shareflag=0  /*in total, 30 control households reported receiving*/
	/*note we did this at the hh level as variable "controlshare"*/
	/*=1 if control group reported having received shared water*/
	replace shareflag=1 if controlgroupsharecontrol_mid==1| controlgroupsharecontrol_end==1
	
	
	/*Matched manually based on name given by trt households*/
	replace shareflag=1 if phoneid==30 ///
		|phoneid==264 ///
		|phoneid==179 ///
		|phoneid==219 ///
		|phoneid==697 ///
		|phoneid==258 ///
		|phoneid==4 ///
		|phoneid==63 ///
		|phoneid==215 ///
		|phoneid==287 ///
		|phoneid==255 ///
		|phoneid==218 ///
		|phoneid==266 ///
		|phoneid==223
		
		preserve
			keep if sample$i==1 & Treat==0
			collapse (mean) shareflag, by(phoneid)
			tab shareflag  
		restore
		
		

/*Set up panel*/
xtset, clear   
xtset phoneid start  








/******************Affect*************************************************
**************************************************************************/


/*Recoding affect variables as 0-100
*for 1 - 7, 6 intervals, 0,16.7,33.3,50,66.7,83.3,100
*happysad coded opposite so higher numbers are more happy, more sociable, more energetic
*for 1-4 (safe), 0, 33.3,66.7,100*/

tab happysad /*1=very happy, 7 = very sad*/
	recode happysad (1 = 100) (2=83.3) (3=66.7) (4=50) (5=33.3) (6=16.7) (7=0), gen(chappysad)
tab lonelysociable /*1=very lonely, 7=very sociable*/
	recode lonelysociable (1 = 0) (2=16.7) (3=33.3) (4=50) (5=66.7) (6=83.3) (7=100), gen(csociable)
tab tiredenergetic /*1=very tired, 7=very energetic*/
	recode tiredenergetic (1 = 0) (2=16.7) (3=33.3) (4=50) (5=66.7) (6=83.3) (7=100), gen(cenergetic)
tab wishdoelse /*1=yes, 2=no*/
	recode wishdoelse (2=0) (3=.)
tab pain /*1=none, 2=slight pain, 3=severe*/
	recode pain (1=0) (2=1) (3=1), gen(somepain)
	label var somepain "=1 if Slight or severe phys pain in last hour"
	recode pain (1=0) (2=0) (3=1), gen(severepain)
	label var severepain "=1 if severe phys pain in last hour"
tab safe  /*1=very unsafe, 2=somewhat unsafe, 4=very safe*/
	recode safe (1=0) (2=33.3) (3=66.7) (4=100), gen(csafe)
tab whowith /*1=alone, 2=with friend, 3=with relative, 4=with neighbor or stranger*/
	recode whowith (2=0) (3=0) (4=0), gen(alone)
	recode whowith (1=0) (2=1) (3=0) (4=0), gen(withfriend)
	label var alone "=1 if alone"
	label var withfriend "=1 if with friend, 0 if alone, w relative, neighbor or stranger"

	
label var chappysad "Happy"
label var csociable "Sociable"
label var cenergetic "Energetic"
label var csafe "Safe"
label var somepain "Some Pain"
label var wishdoelse "Wish Do Else"


/*Table 3: Affect - preintervention balance and intervention summary stats*/
	preserve
		keep if startday<td(8sept2016) & sample$i==1
		codebook phoneid
	balancetable Treat chappysad csociable cenergetic csafe ///
		somepain wishdoelse  ///
		using textables/affect_balance.tex, ///
		ctitles("Control" "Treatment" "Difference") ///
		format(%5.2f) replace varlabels wrap
	restore

	preserve
	keep if startday>td(26sept2016) & sample$i==1
		codebook phoneid
	balancetable Treat chappysad csociable cenergetic csafe ///
		somepain wishdoelse  ///
		using textables/affect_balance_post.tex, ///
		ctitles("Control" "Treatment" "Difference") ///
		format(%5.2f) replace varlabels wrap
	restore
	



	
/*Table 5: Affect DID Intent to Treat, drops 2 weeks of partial treatment*/
			
		drop if dow==0  /*4 sunday records*/	
			
	/*Without Romano-Wolf*/
	foreach outcome in chappysad csociable cenergetic csafe somepain wishdoelse {
		 reg `outcome' Treat POSTITT POSTITT_TRT aft even i.dow i.week ///
			if sample$i==1,vce(cluster phoneid)
		estadd local DOW "Yes"
		estadd local Week "Yes"
		estadd scalar LowCI=_b[POSTITT_TRT] - 1.96* _se[POSTITT_TRT]
		estadd scalar HighCI=_b[POSTITT_TRT]+1.96* _se[POSTITT_TRT]
		sum `outcome' if Treat==0
		estadd scalar meancontrol = r(mean)
		est sto DID`outcome'
		}
		
		/*Output to Latex*/
		esttab DIDchappysad DIDcsociable DIDcenergetic DIDcsafe DIDsomepain DIDwishdoelse ///
			using "textables/DIDaffectWeekDOW.tex", replace  ///
			keep(Treat POSTITT POSTITT_TRT aft even) ///
			order(POSTITT_TRT Treat POSTITT aft even) ///
			b(a2)  p  /*b(a3) tells it to report three sig digits on beta*/ ///
			label  nonote nogaps ///
			stats(N meancontrol DOW Week LowCI HighCI, ///
			      labels("ESM Observations" "Mean in control group" "Day-of-Week FEs" "Week FEs" "95\% 				CI lower" "95\% CI upper") ///
			      fmt(%9.0fc a2 %3s %3s a2 a2 )) ///
				star(* 0.10 ** 0.05 *** 0.01)
		

	
/*Simple treatment-control diﬀerence in the post-treatment period.*/

est clear

foreach outcome in chappysad csociable cenergetic csafe somepain wishdoelse {
	 reg `outcome' Treat aft even i.dow i.week ///
				if POSTITT==1 & sample$i==1, vce(cluster phoneid)
	estadd local DOW "Yes"
	estadd local Week "Yes"
	estadd scalar LowCI=_b[Treat] - 1.96* _se[Treat]
	estadd scalar HighCI=_b[Treat]+1.96* _se[Treat]
	sum `outcome' if Treat==0
	estadd scalar meancontrol = r(mean)
	est sto FD`outcome'
	}

	
		/*Output to Latex*/
		esttab FDchappysad FDcsociable FDcenergetic FDcsafe FDsomepain FDwishdoelse ///
			using "textables/FDaffectWeekDOW.tex", replace  ///
			keep(Treat aft even) ///   
			b(a2)  p ///
			label  nonote nogaps ///
			stats(N meancontrol DOW Week LowCI HighCI, ///
			      labels("ESM Observations" "Mean in control group" "Day-of-Week FEs" "Week FEs" "95\% 			CI lower" "95\% CI upper") ///
			      fmt(%9.0fc a2 %3s %3s a2 a2 )) ///
			 star(* 0.10 ** 0.05 *** 0.01)
			
			
			
/*Appendix Table A5: Lee bounds on simple treatment-control difference*/
			foreach outcome in chappysad csociable cenergetic csafe somepain wishdoelse {
				leebounds `outcome' Treat if POSTITT==1 & sample$i==1
				est sto Lee`outcome'
				}
			
			/*Output to Latex*/
			esttab Leechappysad Leecsociable Leecenergetic Leecsafe Leesomepain Leewishdoelse ///
			using "textables/Lee_affect.tex", replace  ///
			keep(lower upper) ///   
			b(a2)   p  /*b(a3) tells it to report three sig digits on beta*/ ///
			stats(N, ///
					labels("ESM Observations") ///
					fmt(%9.0fc)) ///
			label  nonote nogaps ///
			star(* 0.10 ** 0.05 *** 0.01)

			
/*Appendix Table A3. Sensitivity on ITT: drop control hhs who received shared water */
est clear
foreach outcome in chappysad csociable cenergetic csafe somepain wishdoelse {
	reg `outcome' Treat POSTITT POSTITT_TRT aft even i.dow i.week ///
		if shareflag==0 & sample$i==1,vce(cluster phoneid)
			estadd local DOW "Yes"
			estadd local Week "Yes"
			estadd scalar LowCI=_b[POSTITT_TRT] - 1.96* _se[POSTITT_TRT]
			estadd scalar HighCI=_b[POSTITT_TRT]+1.96* _se[POSTITT_TRT]
			sum `outcome' if Treat==0
			estadd scalar meancontrol = r(mean)
			est sto DID`outcome'
	}
		/*Output to Latex*/
		esttab DIDchappysad DIDcsociable DIDcenergetic DIDcsafe DIDsomepain DIDwishdoelse ///
			using "textables/DIDaffectWeekDOWNoShare.tex", replace  ///
			keep(Treat POSTITT POSTITT_TRT aft even) ///
			order(POSTITT_TRT Treat POSTITT) ///
			b(a2)  p   /*b(a3) tells it to report three sig digits on beta*/ ///
			label  nonote nogaps ///
			stats(N meancontrol DOW Week LowCI HighCI, ///
			      labels("ESM Observations" "Mean in control group" "Day-of-Week FEs" "Week FEs" "95\% CI lower" "95\% CI upper") ///
			      fmt(%9.0fc a2 %3s %3s a2 a2 )) ///
			star(* 0.10 ** 0.05 *** 0.01)

	

est clear
/*Appendix Table A7: Sensitivity on ITT: include over-submitters*/
foreach outcome in chappysad csociable cenergetic csafe somepain wishdoelse {
	reg `outcome' Treat POSTITT POSTITT_TRT aft even i.dow i.week ///
			if sample1==1,vce(cluster phoneid)
	estadd local DOW "Yes"
	estadd local Week "Yes"
	estadd scalar LowCI=_b[POSTITT_TRT] - 1.96* _se[POSTITT_TRT]
	estadd scalar HighCI=_b[POSTITT_TRT]+1.96* _se[POSTITT_TRT]
	sum `outcome' if Treat==0
	estadd scalar meancontrol = r(mean)
	est sto DID`outcome'
	}
		*Output to Latex
		esttab DIDchappysad DIDcsociable DIDcenergetic DIDcsafe DIDsomepain DIDwishdoelse ///
			using "textables/DIDaffectWeekDOWAllRecs.tex", replace  ///
			keep(Treat POSTITT POSTITT_TRT aft even) ///
			order(POSTITT_TRT Treat POSTITT) ///
			b(a2)  p  /*b(a3) tells it to report three sig digits on beta*/ ///
			label  nonote nogaps ///
			stats(N meancontrol DOW Week LowCI HighCI, ///
			      labels("ESM Observations" "Mean in control group" "Day-of-Week FEs" "Week FEs" "95\% CI lower" "95\% CI upper") ///
			      fmt(%9.0fc a2 %3s %3s a2 a2 )) ///
			star(* 0.10 ** 0.05 *** 0.01)
		

	
	

/******************Time *************************************************
**************************************************************************/
	

*Create time dummies, detailed categories
gen meetings=(maintime==1)
	label var meetings "Attending meetings"
gen wood=(maintime==2)
	label var wood "Collecting firewood"
gen sleeping=(maintime==3)
	label var sleeping "Sleeping"
gen otherwork=(maintime==4)
	label var otherwork "Other work"
gen study=(maintime==5)
	label var study "Studying"
gen sports=(maintime==6)
	label var sports "Sports, hobbies"
gen idle=(maintime==7)
	label var idle "Idle"
gen washing=(maintime==8)
	label var washing "Bathing, Pers. Care"
gen school=(maintime==9)
	label var school "Attending School"
gen caring=(maintime==10)
	label var caring "Caring for others"
gen radio=(maintime==11)
	label var radio "Radio or TV"
gen social=(maintime==12)
	label var social "Socializing"
gen water=(maintime==13)
	label var water "Collecting water"
gen private=(maintime==14)
	label var private "Private"
gen market=(maintime==15)
	label var market "Going to market"
gen livestock=(maintime==16)
	label var livestock "Tending livestock"
gen farming=(maintime==17)
	label var farming "Farming"
gen hhwork=(maintime==18)
	label var hhwork "Household work"
	
	


*******Distinguishing between types of work*****************
rename ownfarm ownfarm_base
gen ownfarm=0
	replace ownfarm=1 if farming==1 & ownfarm_prim==1
	label var ownfarm "Farming, own"
gen informal_unpaid=0
	replace informal_unpaid=1 if farming==1 & ownfarm_prim==2 & paidfarm_prim==2
	replace informal_unpaid=1 if otherwork==1 & otherwork_prim==1
	label var informal_unpaid "Casual labor, unpaid"
gen informal_paid=0
	replace informal_paid=1 if farming==1 & ownfarm_prim==2 & paidfarm_prim==1
	replace informal_paid=1 if otherwork==1 & otherwork_prim==3
	label var informal_paid "Casual labor, paid"
gen formal_paid=0
	replace formal_paid=1 if  otherwork==1 & otherwork_prim==2
	label var formal_paid "Paid formal labor"
gen privbus=0
	replace privbus=1 if  otherwork==1 & otherwork_prim==4
	label var privbus "Work in own business"
gen work = (wood==1|water==1|livestock==1|ownfarm==1| ///
		informal_unpaid==1| informal_paid==1|formal_paid==1| privbus==1)
	
/*Collapsed time use categories, outcomes used in EDCC paper*/
gen paidwork=0
	replace paidwork=1 if informal_paid==1|formal_paid==1|privbus==1
	label var paidwork "Paid work"
	*recall in "ESM analysis" i distinguish own farm labor vs. paid or unpaid off farm
gen leisure=0
	for any sleeping sports idle radio private social: replace leisure=1 if X==1
	label var leisure "Leisure + social"
gen readstudyschool=0
	for any school study: replace readstudyschool=1 if X==1
	label var readstudyschool "Read + Study"
gen hhworkgr = 0
	for any hhwork caring livestock : replace hhworkgr=1 if X==1
	label var hhworkgr "Household Work"
gen other =0
	for any market meetings informal_unpaid washing: replace other=1 if X==1
	label var other "Other"

	

	
/*Table 4: Balance pre-randomization and mean outcomes during treatment*/

	/*Time Use*/
	preserve
	keep if startday<td(8sept2016) & sample$i==1
	balancetable Treat water hhworkgr leisure ownfarm paidwork readstudyschool wood ///
		using textables/time_balance_pre.tex, ///
		ctitles("Control" "Treatment" "Difference") ///
		format(%7.2f) replace varlabels wrap
	restore


	preserve
	keep if startday>td(26sept2016) & sample$i==1
	balancetable Treat water hhworkgr leisure ownfarm paidwork readstudyschool wood ///
		using textables/time_balance_post.tex, ///
		ctitles("Control" "Treatment" "Difference") ///
		format(%7.2f) replace varlabels wrap
	restore

	



global time_all "water wood hhworkgr leisure ownfarm paidwork readstudyschool"

/*Table 6: Time use DiD ITT */

foreach outcome in $time_all {
	reg `outcome' Treat POSTITT POSTITT_TRT i.dow i.week if sample$i==1, vce(cluster phoneid)
	estadd local DOW "Yes"
	estadd local Week "Yes"
	estadd scalar LowCI=_b[POSTITT_TRT]*930 - 1.96* _se[POSTITT_TRT]*930
	estadd scalar HighCI=_b[POSTITT_TRT]*930 +1.96* _se[POSTITT_TRT]*930
	sum `outcome' if Treat==0
	estadd scalar meancontrol = r(mean)
	est sto DID`outcome'
	}
	/*Output to Latex*/
		esttab DIDwater DIDhhworkgr DIDleisure DIDownfarm DIDpaidwork DIDreadstudyschool DIDwood  ///
			using "textables/DIDtimeWeekDOW.tex", replace  ///
			keep(POSTITT_TRT Treat POSTITT) ///
			order(POSTITT_TRT Treat POSTITT) ///
			b(a2)  p /*b(a3) tells it to report three sig digits on beta*/ ///
			stats(N meancontrol DOW Week LowCI HighCI, ///
			      labels("ESM Observations" "Mean in control group" "Day-of-Week FEs" "Week FEs" "95\% CI lower (mins)" "95\% CI upper(mins)") ///
			      fmt(%9.0fc a2 %3s %3s a2 a2 )) ///
			label  nonote nogaps ///
			star(* 0.10 ** 0.05 *** 0.01)

/*Appendix Table A4 Sensitivity: drop control households who received shared water*/
est clear
foreach outcome in $time_all {
	reg `outcome' Treat POSTITT POSTITT_TRT i.dow i.week if sample$i==1 ///
									& shareflag==0, vce(cluster phoneid)
	estadd local DOW "Yes"
	estadd local Week "Yes"
	estadd scalar LowCI=_b[POSTITT_TRT]*930 - 1.96* _se[POSTITT_TRT]*930
	estadd scalar HighCI=_b[POSTITT_TRT]*930 +1.96* _se[POSTITT_TRT]*930
	sum `outcome' if Treat==0
	estadd scalar meancontrol = r(mean)
	est sto DID`outcome'

	}
		/*Output to Latex*/
		esttab DIDwater DIDhhworkgr DIDleisure DIDownfarm DIDpaidwork DIDreadstudyschool DIDwood  ///
			using "textables/DIDtimeWeekDOWNoShare.tex", replace  ///
			keep(POSTITT_TRT Treat POSTITT) ///
			order(POSTITT_TRT Treat POSTITT) ///
			b(a2)  p  /*b(a3) tells it to report three sig digits on beta*/ ///
			stats(N meancontrol DOW Week LowCI HighCI, ///
			      labels("ESM Observations" "Mean in control group" "Day-of-Week FEs" "Week FEs" "95\% CI lower (mins)" "95\% CI upper(mins)") ///
			      fmt(%9.0fc a2 %3s %3s a2 a2 )) ///
			label  nonote nogaps ///
			star(* 0.10 ** 0.05 *** 0.01)
			
/*Guilt-driven water sharing*/
ttest water if sample0==1 & POSTITT==1 & Treat==1, by(trtshare)
di (0.0094 - 0.0076)*930

/*Appendix Table A8: Sensitivity: include over-submitter*/
est clear
foreach outcome in $time_all {
	reg `outcome' Treat POSTITT POSTITT_TRT i.dow i.week if sample1==1 ///
									& shareflag==0, vce(cluster phoneid)
	estadd local DOW "Yes"
	estadd local Week "Yes"
	estadd scalar LowCI=_b[POSTITT_TRT]*930 - 1.96* _se[POSTITT_TRT]*930
	estadd scalar HighCI=_b[POSTITT_TRT]*930 +1.96* _se[POSTITT_TRT]*930
	sum `outcome' if Treat==0
	estadd scalar meancontrol = r(mean)
	est sto DID`outcome'

	}
		/*Output to Latex*/
		esttab DIDwater DIDhhworkgr DIDleisure DIDownfarm DIDpaidwork DIDreadstudyschool DIDwood  ///
			using "textables/DIDtimeWeekDOWAllRecs.tex", replace  ///
			keep(POSTITT_TRT Treat POSTITT) ///
			order(POSTITT_TRT Treat POSTITT) ///
			b(a2)  p  /*b(a3) tells it to report three sig digits on beta*/ ///
			stats(N meancontrol DOW Week LowCI HighCI, ///
			      labels("ESM Observations" "Mean in control group" "Day-of-Week FEs" "Week FEs" "95\% CI lower(mins)" "95\% CI upper(mins)") ///
			      fmt(%9.0fc a2 %3s %3s a2 a2 )) ///
			label  nonote nogaps ///
			star(* 0.10 ** 0.05 *** 0.01)


 
			
/*Appendix Table A6 Simple treatment-control diﬀerence in the post-treatment period*/
est clear

foreach outcome in $time_all {
	reg `outcome' Treat i.dow i.week if POSTITT==1 & sample$i==1, vce(cluster phoneid)
	estadd local DOW "Yes"
	estadd local Week "Yes"
	estadd scalar LowCI=_b[Treat]*930 - 1.96* _se[Treat]*930
	estadd scalar HighCI=_b[Treat]*930 +1.96* _se[Treat]*930
	sum `outcome' if Treat==0
	estadd scalar meancontrol = r(mean)
	est sto FD`outcome'
	}

	/*Output to Latex*/
		esttab FDwater FDhhworkgr FDleisure FDownfarm FDpaidwork FDreadstudyschool FDwood ///
			using "textables/FDtimeWeekDOW.tex", replace  ///
			keep(Treat) ///
			b(a2)  p   /*b(a3) tells it to report three sig digits on beta*/ ///
			stats(N meancontrol DOW Week LowCI HighCI, ///
			      labels("ESM Observations" "Mean in control group" "Day-of-Week FEs" "Week FEs" "95\% CI lower (mins)" "95\% CI upper(mins)") ///
			      fmt(%9.0fc a2 %3s %3s a2 a2 )) ///
			label  nonote nogaps ///
			star(* 0.10 ** 0.05 *** 0.01)



	
	

	
	
 





/*FWER Multiple Hypothesis Testing, for Tables 5 and 6*/

/*reps set to 10 for demonstration; set to 1000 for closer replication (computer intensive)*/
	
/*Romano-Wolf for all affect and time outcomes */

rwolf2 ///
 (reg chappysad 		Treat POSTITT POSTITT_TRT aft even i.dow i.week if sample0==1,vce(cluster phoneid)) ///
 (reg csociable 		Treat POSTITT POSTITT_TRT aft even i.dow i.week if sample0==1,vce(cluster phoneid)) ///
 (reg cenergetic 		Treat POSTITT POSTITT_TRT aft even i.dow i.week if sample0==1,vce(cluster phoneid)) ///
 (reg csafe 			Treat POSTITT POSTITT_TRT aft even i.dow i.week if sample0==1,vce(cluster phoneid)) ///
 (reg somepain 			Treat POSTITT POSTITT_TRT aft even i.dow i.week if sample0==1,vce(cluster phoneid)) ///
 (reg wishdoelse 		Treat POSTITT POSTITT_TRT aft even i.dow i.week if sample0==1,vce(cluster phoneid))  ///
 (reg water 			Treat POSTITT POSTITT_TRT  			i.dow i.week if sample0==1,vce(cluster phoneid)) ///
 (reg hhworkgr 			Treat POSTITT POSTITT_TRT 			i.dow i.week if sample0==1,vce(cluster phoneid)) ///
 (reg leisure 			Treat POSTITT POSTITT_TRT 			i.dow i.week if sample0==1,vce(cluster phoneid)) ///
 (reg ownfarm 			Treat POSTITT POSTITT_TRT 			i.dow i.week if sample0==1,vce(cluster phoneid)) ///
 (reg paidwork 			Treat POSTITT POSTITT_TRT 			i.dow i.week if sample0==1,vce(cluster phoneid)) ///
 (reg readstudyschool 	Treat POSTITT POSTITT_TRT 			i.dow i.week if sample0==1,vce(cluster phoneid)) ///
 (reg wood 				Treat POSTITT POSTITT_TRT 			i.dow i.week if sample0==1,vce(cluster phoneid)), ///
  indepvars(POSTITT_TRT,POSTITT_TRT,POSTITT_TRT,POSTITT_TRT,POSTITT_TRT,POSTITT_TRT, ///
  POSTITT_TRT,POSTITT_TRT,POSTITT_TRT,POSTITT_TRT,POSTITT_TRT,POSTITT_TRT,POSTITT_TRT) ///
 verbose seed(1234) reps(10) holm cluster(phoneid) idcluster(newrf2) 
 
/*Get Westfall-Young approach to FWER, not reported in EDCC paper  
* net install wyoung, from("https://raw.githubusercontent.com/reifjulian/wyoung/controls-option") replace
 
 /*the Westfall-Young FWER command can't handle time series operators, so making 
	them manually just for that command*/
	forv i=1/6 {
		gen dow`i'=(dow==`i')
		}
	forv i=35/41 {
		gen week`i'=(week==`i')
		}
  

wyoung, ///
 cmd("reg chappysad		Treat POSTITT POSTITT_TRT aft even 	dow1 dow2 dow3 dow4 dow5 week35 week36 week37 week38 week39 week40 if sample0==1, vce(cluster phoneid)" ///
 "reg csociable			Treat POSTITT POSTITT_TRT aft even 	dow1 dow2 dow3 dow4 dow5 week35 week36 week37 week38 week39 week40 if sample0==1, vce(cluster phoneid)" ///
 "reg cenergetic 		Treat POSTITT POSTITT_TRT aft even 	dow1 dow2 dow3 dow4 dow5 week35 week36 week37 week38 week39 week40 if sample0==1, vce(cluster phoneid)" ///
 "reg csafe 			Treat POSTITT POSTITT_TRT aft even 	dow1 dow2 dow3 dow4 dow5 week35 week36 week37 week38 week39 week40 if sample0==1, vce(cluster phoneid)" ///
 "reg somepain 			Treat POSTITT POSTITT_TRT aft even 	dow1 dow2 dow3 dow4 dow5 week35 week36 week37 week38 week39 week40 if sample0==1, vce(cluster phoneid)" ///
 "reg wishdoelse 		Treat POSTITT POSTITT_TRT aft even 	dow1 dow2 dow3 dow4 dow5 week35 week36 week37 week38 week39 week40 if sample0==1, vce(cluster phoneid)" /// 
 "reg water 			Treat POSTITT POSTITT_TRT dow1 dow2 dow3 dow4 dow5 week35 week36 week37 week38 week39 week40 if sample0==1, vce(cluster phoneid)" ///
 "reg hhworkgr 			Treat POSTITT POSTITT_TRT dow1 dow2 dow3 dow4 dow5 week35 week36 week37 week38 week39 week40 if sample0==1, vce(cluster phoneid)" ///
 "reg leisure 			Treat POSTITT POSTITT_TRT dow1 dow2 dow3 dow4 dow5 week35 week36 week37 week38 week39 week40 if sample0==1, vce(cluster phoneid)" ///
 "reg ownfarm 			Treat POSTITT POSTITT_TRT dow1 dow2 dow3 dow4 dow5 week35 week36 week37 week38 week39 week40 if sample0==1, vce(cluster phoneid)" ///  
 "reg paidwork 			Treat POSTITT POSTITT_TRT dow2 dow3 dow4 dow5 week35 week36 week37 week38 week39 week40 if sample0==1, vce(cluster phoneid)" ///
 "reg readstudyschool 	Treat POSTITT POSTITT_TRT dow1 dow2 dow3 dow4 dow5 week35 week36 week37 week38 week39 week40 if sample0==1, vce(cluster phoneid)" ///
 "reg wood 				Treat POSTITT POSTITT_TRT dow1 dow2 dow3 dow4 dow5 week35 week36 week37 week38 week39 week40 if sample0==1, vce(cluster phoneid)") ///
     familyp(POSTITT_TRT) cluster(phoneid) seed(1234) bootstraps(10)
 
*/

 
 /*Non-experimental regressions on affect */

 est clear
foreach outcome in chappysad csociable cenergetic csafe  {
	xtreg `outcome' $time_all alone aft even if sample$i==1 & startday<td(8sept2016), ///
			fe vce(cluster phoneid)
		est sto Modl`outcome'
	}
	
xtlogit somepain $time_all alone aft even if sample$i==1 & startday<td(8sept2016), fe
	/*figure out how to do clustered st errors here!And marginal effects!*/
	est sto Modlsomepain

xtlogit wishdoelse $time_all alone aft even if sample$i==1 & startday<td(8sept2016), fe
	est sto Modlwishdo
	
esttab Modlchappysad Modlcsociable Modlcenergetic Modlcsafe ///
			using "textables/affectregsRCT.tex", replace ///
			b(a2) se  /*b(a3) tells it to report three sig digits on beta*/ ///
			mtitles("Happy" "Sociable" "Energetic" "Safe") ///
			label  nonote nogaps ///
			star(* 0.10 ** 0.05 *** 0.01)

/*Figure 3: Non-experimental affect coefficient plots */	
coefplot (Modlchappysad), ///
	keep(water wood hhworkgr leisure ownfarm paidwork readstudyschool alone aft even) ///
	xline(0) msymbol(d) mfcolor(white) levels(95 90) 	///
	scheme(s1mono) ///
	title("A. Happy") ///
	name(A, replace) nodraw
	
coefplot (Modlcenergetic), ///
	keep(water wood hhworkgr leisure ownfarm paidwork readstudyschool alone aft even) ///
	xline(0) msymbol(d) mfcolor(white) levels(95 90) 	///
	scheme(s1mono) ///
	title("B. Energetic")  ///
	name(B, replace) nodraw
	
coefplot (Modlcsociable), ///
	keep(water wood hhworkgr leisure ownfarm paidwork readstudyschool alone aft even) ///
	xline(0) msymbol(d) mfcolor(white) levels(95 90)	///
	scheme(s1mono) ///
	title("C. Sociable")  ///
	name(C, replace) nodraw
	
coefplot (Modlcsafe), ///
	keep(water wood hhworkgr leisure ownfarm paidwork readstudyschool alone aft even) ///
	xline(0) msymbol(d) mfcolor(white) levels(95 90) 	///
	scheme(s1mono) ///
	title("D. Safe")  ///
	name(D, replace)  nodraw	
				
			
coefplot (Modlsomepain), ///
	keep(water wood hhworkgr leisure ownfarm paidwork readstudyschool alone aft even) ///
	xline(0) msymbol(d) mfcolor(white) levels(95 90) nodraw 	///
	scheme(s1mono) ///
	title("E. Some or severe pain") ///
	name(E, replace)
	

coefplot (Modlwishdo), ///
	keep(water wood hhworkgr leisure ownfarm paidwork readstudyschool alone aft even) ///
	xline(0) msymbol(d) mfcolor(white) levels(99 95) nodraw	///
	scheme(s1mono) ///
	title("F. Wish do something else") ///
	name(F, replace)
	
gr combine A B C D E F, cols(2) xsize(10) ysize(8) iscale(0.5) scheme(s1mono)
				gr export "texfigures\affectregsRCT.pdf", replace
				gr export "texfigures\affectregsRCT.emf", as(emf) replace
			
	
 
 
 

/*************************************************************/
/*Analysis of School-children: School-reported attendance*/
/*************************************************************/

est clear

use "SchoolAttendance_DeId", clear

 /*"Post" and "Post*treat" variables for diff-in-diff*/
	
	/*Excludes two weeks of partial vending, sets to missing for control & trt*/
	gen POSTITT = startday>=td(26sept2016)  
			replace POSTITT=. if startday>=td(12sept2016) & startday<=td(26sept2016)  
			label var POSTITT "Post"
	gen POSTITT_TRT = Treat*POSTITT
			label var POSTITT_TRT "Post*Treat"

gen girl  = (gender==1)
gen AGE5_9 = (age<=9)
gen AGE10_15 = (age>=10 & age<=15)
gen AGE16_19 = (age>=16)
gen treat_girl = girl*POSTITT_TRT
gen treat_AGE5_9 = AGE5_9* POSTITT_TRT
gen treat_AGE10_15 = AGE10_15*POSTITT_TRT
gen treat_AGE16_19 = AGE16_19*POSTITT_TRT
gen treat_age = age*POSTITT_TRT



/* balance*/
	preserve
		collapse (mean) Treat, by(phoneid)
		tab Treat  /*records from 79 control, 75 trt hhs*/
	restore
	
	preserve
		collapse (mean) Treat AGE*, by(phoneid pid)
		egen id = group(phoneid pid)
		tab Treat  /*records on 118 control children, 124 trt children*/
		for any AGE5_9 AGE10_15 AGE16_19: tab X
	restore
	
	/*Baseline balance in attendance*/
	by Treat,sort: sum attend if startday < td(8sept2016)
	ttest attend if startday < td(8sept2016), by(Treat)



egen id = group(phoneid pid)
xtset id startday

label var attend "Attendance (school-reported)"
tab attend
sum attend if Treat==0

/*for Table 7: ITT*/
reg attend Treat POSTITT POSTITT_TRT i.week i.dow, vce(cluster phoneid)
		estadd local DOW "Yes"
		estadd local Week "Yes"
		estadd scalar LowCI=_b[POSTITT_TRT]- 1.96* _se[POSTITT_TRT]
		estadd scalar HighCI=_b[POSTITT_TRT]+1.96* _se[POSTITT_TRT]
		sum attend if Treat==0
		estadd scalar meancontrol = r(mean)
		eststo DIDattendlist
		
/*Age and gender interactions, not reported*/	
reg attend Treat POSTITT POSTITT_TRT i.week i.dow ///
		treat_girl treat_AGE5_9 treat_AGE10_15 treat_AGE16_19, vce(cluster phoneid)
		estadd local DOW "Yes"
		estadd local Week "Yes"
		eststo DIDattendlistinteract
		


/*************************************************************/
/*Analysis of daily surveys of school-children*/
/*************************************************************/

 use SchoolKids_DeID, clear


 /*"Post" and "Post*treat" variables for diff-in-diff*/
	
	/*Excludes two weeks of partial vending, sets to missing for control & trt*/
	gen POSTITT = startday>=td(26sept2016)  
			replace POSTITT=. if startday>=td(12sept2016) & startday<=td(26sept2016)  
			label var POSTITT "Post"
	gen POSTITT_TRT = Treat*POSTITT
			label var POSTITT_TRT "Post*Treat"
			
		

/*day of week control*/
gen dow=dow(startday)
gen week=week(startday)	
	
/*drop first weeks of ramp-up, to match RCT*/
tab startday
drop if startday<td(29august2016)

	
xtset, clear   
xtset id start

codebook id  /*3,791records*/

	
gen AGE5_9 = (age<=9)
gen AGE10_15 = (age>=10 & age<=15)
gen AGE16_19 = (age>=16)
gen posttreat_girl = POSTITT_TRT
gen posttreat_AGE5_9 = AGE5_9 * POSTITT_TRT
gen posttreat_AGE10_15 = AGE10_15 *POSTITT_TRT
gen posttreat_AGE16_19 =  AGE16_19 *POSTITT_TRT
gen posttreat_age = age*POSTITT_TRT


gen weekofyear=week(startday)


label var attend "Self-reported attendance"
label var studymins "Mins spent study"
label var nochores "No Chores"
label var water "Water collection"
label var firewood "Wood collection"
label var clean "Cleaning"
label var cook "Cooking"
label var farmlivest "Farm/livestock"
	
/*Appendix Table A1:Balance pre-randomization and mean outcomes during treatment*/
	preserve
	keep if startday<td(8sept2016)  
	balancetable Treat attend studymins nochores water firewood clean ///
		cook farmlivest ///
		using textables/kid_balance_pre.tex, ///
		ctitles("Control" "Treatment" "Difference") ///
		format(%7.2g) replace varlabels wrap
	restore


	preserve
	keep if startday>td(26sept2016) 
	balancetable Treat attend studymins nochores water firewood clean ///
		cook farmlivest ///
		using textables/kid_balance_post.tex, ///
		ctitles("Control" "Treatment" "Difference") ///
		format(%7.2g) replace varlabels wrap
	restore
	

	
	/*Table 7: ITT DiD*/

	foreach outcome in water attend studymins nochores firewood clean cook farmlivest {
		reg `outcome' Treat POSTITT POSTITT_TRT i.dow i.week, vce(cluster phoneid)
		sum `outcome' if Treat==0
		estadd local DOW "Yes"
		estadd local Week "Yes"
		estadd scalar LowCI=_b[POSTITT_TRT]- 1.96* _se[POSTITT_TRT]
		estadd scalar HighCI=_b[POSTITT_TRT]+1.96* _se[POSTITT_TRT]
		sum `outcome' if Treat==0
		estadd scalar meancontrol = r(mean)
		est sto DID`outcome'
	}
	
		/*Output to Latex*/
		esttab DIDattend DIDattendlist DIDstudymins DIDwater DIDnochores DIDfirewood DIDclean DIDcook DIDfarmlivest ///
			using "textables/DIDKidsWeekDOW.tex", replace  ///
			keep(Treat POSTITT POSTITT_TRT) ///
			order(POSTITT_TRT treat POSTITT) ///
			mtitle("Attend(self)" "Attend(school)" "Study Mins" "Water" "No Chores" "Firewood" "Clean" "Cook" "FarmAnim") ///
			b(a2)  p  /*b(a3) tells it to report three sig digits on beta*/ ///
			label  nonote nogaps ///
			stats(N meancontrol DOW Week LowCI HighCI, ///
			      labels("Observations" "Mean in control group" "Day-of-Week FEs" "Week FEs" "95\% CI lower" "95\% CI upper") ///
			      fmt(%9.0fc a2 %3s %3s a2 a2 )) ///
			star(* 0.10 ** 0.05 *** 0.01)
		
		

/*Romano-Wolf for kid outcomes, set to reps= 1000 */
rwolf2 ///
 (reg water 		Treat POSTITT POSTITT_TRT i.dow i.week,vce(cluster phoneid)) ///
 (reg attend 		Treat POSTITT POSTITT_TRT i.dow i.week,vce(cluster phoneid)) ///
 (reg studymins 		Treat POSTITT POSTITT_TRT i.dow i.week,vce(cluster phoneid)) ///
 (reg nochores 			Treat POSTITT POSTITT_TRT i.dow i.week,vce(cluster phoneid)) ///
 (reg firewood 			Treat POSTITT POSTITT_TRT i.dow i.week,vce(cluster phoneid)) ///
 (reg clean 		Treat POSTITT POSTITT_TRT i.dow i.week,vce(cluster phoneid)) ///
 (reg cook 			Treat POSTITT POSTITT_TRT i.dow i.week,vce(cluster phoneid)) ///
 (reg farmlivest 			Treat POSTITT POSTITT_TRT i.dow i.week,vce(cluster phoneid)), ///
  indepvars(POSTITT_TRT,POSTITT_TRT,POSTITT_TRT,POSTITT_TRT,POSTITT_TRT,POSTITT_TRT, ///
  POSTITT_TRT,POSTITT_TRT) ///
 verbose seed(123) reps(10) cluster(phoneid) idcluster(newrf2)  
 



